<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">      
      <el-form-item label="班级" prop="classId">
        <el-select v-model="queryParams.classId" filterable clearable placeholder="请选择班级">
          <el-option
            v-for="item in clazzList"
            :key="item.id"
            :label="item.title"
            :value="item.id"
            size="small">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">确定</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="evalScoreList">
      <el-table-column label="班级" align="center" prop="className" />
      <el-table-column label="学生" align="center" prop="stuName" />
      <el-table-column label="学号" align="center" prop="stuCode" />
      <el-table-column label="得分" align="center" prop="evaluateScore" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['evaluate:evalScore:add']"
          >评分</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改评教分数对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" label-width="80px">
        <el-table
          v-loading="loading"
          :data="evalNormList"
          row-key="id"
          :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
        >
          <el-table-column label="指标名称" prop="title" width="300px" />
          <el-table-column label="指标分数" align="center" prop="score" />
          <el-table-column label="打分" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-input-number size="mini" :min="scope.row.min" :max="scope.row.max" :precision="1" :step="1" v-model="form['norm'+scope.row.id]" placeholder="请输入分数" v-if="!scope.row.children || scope.row.children.length == 0"/>
            </template>
          </el-table-column>
        </el-table>
        <el-divider></el-divider>
        <el-form-item label="老师寄语" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入您想对学生说的话" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listEvalScore, saveEvalScore } from "@/api/evaluate/evalScore";
import { getEvalProject} from "@/api/evaluate/evalProject";
import { listEvaluate } from "@/api/evaluate/evalResults";
import { selectEvalNorm } from "@/api/evaluate/evalNorm";
import { listMasterClazz} from "@/api/base/clazz";

export default {
  name: "StudentEvalScore",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 评教分数表格数据
      evalScoreList: [],
      // 班级数据
      clazzList: [],
      // 评教指标表格数据
      evalNormList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      task: null,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        semId: null,
        classId: null,
        teacherId: null,
        courseId: null,
        studentId: null
      },
      // 表单参数
      form: {}
    };
  },
  created() {
    const taskId = this.$route.params && this.$route.params.taskId;
    getEvalProject(taskId).then(response => {
      this.task = response.data;
      this.queryParams.semId = response.data.semId;
      this.getList();
    });
    listMasterClazz({'status':1}).then(response => {
      this.clazzList = response.rows;
    });
    selectEvalNorm({'status':1}).then(response => {
      this.evalNormList = this.handleTree(response.data, "id", "pid");
      this.loading = false;
    });
  },
  methods: {
    /** 查询评教分数列表 */
    getList() {
      if(!this.queryParams.classId) return;

      this.loading = true;
      listEvaluate(this.queryParams).then(response => {
        this.evalScoreList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        semId: null,
        classId: null,
        teacherId: null,
        stuId: null,
        score: null,
        remark: null
      };
      for(var i=0;i<this.evalNormList.length;i++){
        this.form['norm'+this.evalNormList[i]['id']] = null;
      }
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      listEvalScore({stuId:row.stuId, semId: row.semId}).then(response => {
        for(var i=0;i<response.rows.length;i++){
          if(response.rows[i]['score']) this.form['norm'+response.rows[i]['normId']] = response.rows[i]['score'];
        }
        this.form.stuId = row.stuId;
        this.form.semId = row.semId;
        this.form.remark = row.remark;
        this.open = true;
        this.title = "在线评价";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
            saveEvalScore(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除评教分数编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delEvalScore(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有评教分数数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportEvalScore(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>
